using UnityEngine;

namespace QFramework.Framework.Util
{
    /// <summary>
    /// 引用计数器接口
    /// </summary>
    public interface IRefCounter
    {
        int RefCount { get; }
        void Retain(Object refOwner = null);
        void Release(Object refOwner = null);
    }
    
    /// <summary>
    /// 简易引用计数器
    /// </summary>
    public class SimpleRc : IRefCounter
    {
        public int RefCount { get; private set; }
        
        public void Retain(Object refOwner = null)
        {
            RefCount++;
        }

        public void Release(Object refOwner = null)
        {
            RefCount--;
            
            if (RefCount==0)
            {
                OnZeroRef();
            }
        }

        protected virtual void OnZeroRef()
        {
            
        }
    }
}
